<template>
  <transition name="modal-fade" v-if="visible">
    <div class="modal-mask">
      <div class="modal-box">
        <div class="top">
          <div>{{ title }}</div>
          <div class="close" @click="close">×</div>
        </div>
        <div class="middle">
          <div>{{ content }}</div>
        </div>
        <div class="bottom">
          <button @click="cancel">取消</button>
          <button @click="confirm">确认</button>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  name: "Modal",
  data() {
    return {
      visible: true,
      title: "标题",
      content: "内容",
      success: undefined, // 调用成功
    };
  },
  mounted() {},
  methods: {
    confirm() {
      this.visible = false;
      if (typeof this.success === "function") {
        this.success(true);
      }
    },
    cancel() {
      this.visible = false;
      if (typeof this.success === "function") {
        this.success(false);
      }
    },
    close() {
      this.visible = false;
    },
  },
};
</script>

<style scoped>
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  z-index: 998;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.4);
  transition: all 0.5s;
}
.modal-box {
  box-sizing: border-box;
  min-height: 150px;
  min-width: 350px;
  padding: 15px 30px;
  border-radius: 5px;
  background-color: #fff;
  transition: all 0.5s;
}
.top {
  display: flex;
  justify-content: space-between;
}
.close {
  font-size: 25px;
  cursor: pointer;
}
.middle {
  margin: 5px 0 10px 0;
}
.bottom {
  display: flex;
  flex-direction: row-reverse;
}
button {
  outline: none;
  padding: 8px 12px;
  margin-left: 10px;
  border: none;
  background-color: rgb(64, 158, 255);
  color: white;
  border-radius: 5px;
  cursor: pointer;
}
button:hover {
  opacity: 0.8;
}
.modal-fade-enter,
.modal-fade-leave-to {
  opacity: 0;
}

.modal-fade-enter .modal-box,
.modal-fade-leave-to .modal-box {
  transform: translateY(-100%);
}
</style>
